# replace the 2023 "RP/CL" to "RP", change the dollar format into value formatsalary_2023 <- salary_2023 %>%mutate(Pos =ifelse(`POS.`!="RP/CL",`POS.`,"RP")) %>%mutate(Salary_2023=as.numeric(gsub("\\$", "", gsub(",", "", `BASE SALARY`)))) %>% dplyr::select(Pos, Salary_2023)# change the 2019 data dollar format into value formatsalary_2019 <- salary_2019 %>%mutate(Salary_2019=as.numeric(gsub("\\$", "", gsub(",", "", Salary))))# Group by positionsalary_2023_1 <- salary_2023 %>%group_by(Pos) %>%summarize(salary_sum_2023=sum(as.numeric(Salary_2023)))# calculate totalsalary_2023_1$total_2023 <-sum(salary_2023_1$salary_sum_2023)# calculate percentagesalary_2023_1$percetage_2023 <- salary_2023_1$salary_sum_2023/salary_2023_1$total_2023# Group by positionsalary_2019_1 <- salary_2019 %>%group_by(Pos) %>%summarize(salary_sum_2019=sum(as.numeric(Salary_2019)))# calculate totalsalary_2019_1$total_2019 <-sum(salary_2019_1$salary_sum_2019)# calculate percentagesalary_2019_1$percetage_2019 <- salary_2019_1$salary_sum_2019/salary_2019_1$total_2019# Joion the tablesalary_df <- salary_2019_1 %>%left_join(salary_2023_1,by="Pos")# Replace NA with 0salary_df$salary_sum_2023[is.na(salary_df$salary_sum_2023)] <-0salary_df$percetage_2023[is.na(salary_df$percetage_2023)] <-0# Get the total salary for 2019 and 2023salary_total_2019 <-mean(salary_df$total_2019,na.rm=TRUE)salary_total_2023 <-mean(salary_df$total_2023,na.rm=TRUE)
library(plotly)# Create an interactive pie chart for 2019pie_chart_2019 <-plot_ly(labels =~salary_df$Pos, # Labels from the 'brands' vectorvalues =~salary_df$salary_sum_2019, # Values from the 'market_share' vectortype ='pie', # Specify the chart type to be pietextinfo ='label+percent', # Display labels and percentage on the charttextposition ='inside', # Position the text inside the slicesmarker =list(line =list(color ='#FFFFFF', width =2)), # Set slice bordersheight =sqrt(salary_total_2019)/30, width =sqrt(salary_total_2019)/30)# Adding title and enhancing the layoutpie_chart_2019 <-layout(pie_chart_2019, title ='Salary By Positions in 2019')# Create an interactive pie chart for 2023pie_chart_2023 <-plot_ly(labels =~salary_df$Pos, # Labels from the 'brands' vectorvalues =~salary_df$salary_sum_2023, # Values from the 'market_share' vectortype ='pie', # Specify the chart type to be pietextinfo ='label+percent', # Display labels and percentage on the charttextposition ='inside', # Position the text inside the slicesmarker =list(line =list(color ='#FFFFFF', width =2)), # Set slice bordersheight =sqrt(salary_total_2023)/30, width =sqrt(salary_total_2023)/30)# Adding title and enhancing the layoutpie_chart_2023 <-layout(pie_chart_2023, title ='Salary By Positions in 2023')